<template>
  <div>
    <el-table :data="tableData" style="width: 100%" border v-loading="loadings">
      <el-table-column prop="index" label="序号" width="100">
        <template slot-scope="scope">
          <div>{{ getIndex(scope.$index) }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="projectCode" label="项目编号" width="200">
      </el-table-column>
      <el-table-column prop="projectName" label="项目名称" width="200" ></el-table-column>
      <el-table-column prop="projectPerson" label="项目负责人" width="200">
      </el-table-column>
      <el-table-column prop="startTime" label="项目开始时间" width="200"></el-table-column>
      <el-table-column prop="endTime" label="项目结束时间" width="200" ></el-table-column>
      <el-table-column prop="price" label="项目金额" width="200">
        <template slot-scope="scope">
          <div>{{ formatCurrency(scope.row.price, 2) }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="remainDate" label="项目所剩时间" width="200">
        <template slot-scope="scope">
          <div :style="{ color: dateTotal > 3 ? 'green' : 'red' }">
            <div>{{ scope.row.remainDate }}天</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="lookMember" label="项目成员" width="200">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="lookMember(scope.row)"
            >查看成员</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="projectStatus" label="项目状态" width="200">
        <template slot-scope="scope">
          <div v-if="scope.row.projectStatus == '0'">
            <el-tag>待开发</el-tag>
          </div>
          <div v-else-if="scope.row.projectStatus == '1'">
            <el-tag type="warning">开发中</el-tag>
          </div>
          <div v-else><el-tag type="danger">已验收</el-tag></div>
        </template>
      </el-table-column>
      <el-table-column :fixed="'right'" prop="control" label="操作" width="300">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            v-if="scope.row.projectStatus < 2"
            @click="deletes(scope.row)"
            >删除</el-button
          >
          <el-button
            size="small"
            type="success"
            v-if="scope.row.projectStatus == 0"
            plain
            @click="
              changeProjectStatus(
                scope.row.projectStatus,
                scope.row.projectCode,
                scope.row
              )
            "
            >项目确认开发</el-button
          >
          <el-button
            size="small"
            type="success"
            v-if="scope.row.projectStatus == 1"
            @click="
              changeProjectStatus(
                scope.row.projectStatus,
                scope.row.projectCode,
                scope.row
              )
            "
            plain
            >项目确认验收</el-button
          >
          <el-button
            type="warning"
            @click="edit(scope.row)"
            size="small"
            v-if="scope.row.projectStatus < 2"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <look :code="code" :show="show" @close="close" :mode="'1'" ></look>
    <dialog-view
      :show="shows"
      ref="dialogView"
      :title="title"
      :fileLists="fileList"
      :loadinges="loadinges"
      :codes="codes"
      @dialogClose="dialogClose"
    />
  </div>
</template>

<script>
import { url } from "../../../utils/url";
import dialogView from "./dialogView.vue";
import look from "./lookmember.vue";
export default {
  props: ["data", "loading"],
  data() {
    return {
      tableData: [],
      loadings: false,
      dateTotal: "",
      code: "", //查询code
      show: false,
      shows: false,
      title: "",
      fileList: [],
      loadinges: false,
      codes: "",
    };
  },
  components: {
    look,
    dialogView,
  },
  created() {
    this.tableData = this.data;
  },
  watch: {
    data(newV) {
      this.tableData = newV;
    },
    loading(newV) {
      this.loadings = newV;
    },
  },
  methods: {
    //统计
    getIndex(e) {
      return (this.$parent.pag.pageNo - 1) * this.$parent.pag.pageSize + e + 1;
    },
    //转化两位小数
    formatCurrency(amount, decimalPlaces) {
      // 确保金额是数字
      const number = Number(amount);
      // 使用toFixed()方法确定小数位数
      const formatted = number.toFixed(decimalPlaces);
      // 返回格式化后的金额字符串
      return formatted;
    },
    //
    deletes(e) {
      let obj = {
        code: e.projectCode,
        status:e.projectStatus
      };
      this.$http.get("/deleteProject", { params: obj }).then((res) => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.$nextTick(() => {
            this.$emit("tableRefresh", true);
          });
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //查看成员
    lookMember(e) {
      this.show = true;
      this.code = e.projectCode;
    },
    //关闭
    close(e) {
      if (e == true) {
        this.show = false;
        this.code = "";
      }
    },
    //修改状态
    changeProjectStatus(e, k, row) {
      console.log(row, "7777");
      let index = Number(e);
      if (index < 2) {
        index += 1;
        let obj = {
          code: k,
          status: index,
          member: row.member,
          projectName: row.projectName,
        };
        this.$http.post("/updateProjectStatus", obj).then((res) => {
          console.log(res, "1111");
          if (res.code == 200) {
            this.$message.success("确认状态成功");
            this.$emit("tableRefresh", true);
          }
        });
      }
    },
    //编辑
    edit(e) {
      this.shows = true;
      this.title = "项目-编辑";
      this.codes = e.projectCode;
      this.loadinges = true;
      this.$http(`/getSingeProject/${e.projectCode}`).then((res) => {
        if (res.code == 200) {
          this.tak(
            this.$refs.dialogView.form,
            res.data,
            res.data.memberArr,
            this.$refs.dialogView.checkList
          );
          this.fileList = this.takFile(res.data);
          this.loadinges = false;
        }
      });
    },
    //装载
    tak(e, k, m, p) {
      e = Object.assign(e, k);
      m.forEach((ele) => {
        p.push(ele.id);
      });
    },
    takFile(objs) {
      let arr = [];
      if (objs.projectURL != "") {
        let obj = {
          name: objs.projectURL,
          url: url + objs.projectURL,
        };
        arr.push(obj);
      }

      return arr;
    },
    dialogClose(e) {
      if (e == true) {
        this.shows = false;
        this.$parent.pag.pageNo = 1;
        this.$parent.getlist({ pageNo: this.$parent.pag.pageNo, pageSize: 5 });
      }
    },
  },
};
</script>

<style>
</style>